package kr.pfg.pfgTracer.view
{
	import display.Align;
	
	import events.WindowEvent;
	
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import kr.pfg.socialproject.SocialProject;
	
	import skinfactory.SkinButton;
	import skinfactory.SkinScaleImage;

	/**
	 * 	@author		이근수
	 * 
	 * 	Documents Class의 Window 기본 속성을 제어하는 클래스 
	 */
	public class BaseWindow extends SocialProject
	{
		protected var bg			: SkinScaleImage;
		protected var dragArea		: SkinScaleImage;
		
		protected var btnClose		: SkinButton;
		protected var btnMinimize	: SkinButton;
		protected var btnMaximum 	: SkinButton;
		protected var btnPrevSize 	: SkinButton;
		protected var btnResize		: SkinButton;
		protected var btnInfo		: SkinButton;
		protected var btnOption		: SkinButton;
		
		public function BaseWindow() 
		{
			
		}
		protected function init(): void
		{
			bg 			= new SkinScaleImage( "window_bg" );
			bg.width	= stage.stageWidth;
			bg.height 	= stage.stageHeight;
			
			dragArea 	= new SkinScaleImage( "window_titleBar" );
			dragArea.buttonMode = true;
			dragArea.width = stage.stageWidth;
			
			btnClose 		= new SkinButton( "window_btnClose" );
			btnMinimize 	= new SkinButton( "window_btnMinimum" );
			btnMaximum 		= new SkinButton( "window_btnMaximun" );
			btnPrevSize 	= new SkinButton( "window_btnPrevSize" );
			btnResize 		= new SkinButton( "window_btnResize" );
			btnInfo			= new SkinButton( "window_btnInfo" );
			btnOption		= new SkinButton( "window_btnOption" );

			dragArea.addEventListener	( MouseEvent.MOUSE_DOWN, onDragEventListener );
			btnClose.addEventListener	( MouseEvent.CLICK, onMouseEventHandler );
			btnMinimize.addEventListener( MouseEvent.CLICK, onMouseEventHandler );
			btnPrevSize.addEventListener( MouseEvent.CLICK, onMouseEventHandler );
			btnResize.addEventListener	( MouseEvent.MOUSE_DOWN, onResizeListener );
			btnMaximum.addEventListener	( MouseEvent.CLICK, onMouseEventHandler );
			btnInfo.addEventListener	( MouseEvent.CLICK, onMouseEventHandler );
			btnOption.addEventListener	( MouseEvent.CLICK, onMouseEventHandler );
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			btnPrevSize.visible = false;
			
			stage.nativeWindow.minSize = new Point( 300, 200 );
		}
		protected function setPosition(): void
		{ 
			btnClose.x = bg.width - btnClose.width - 5;
			btnClose.y = Align.getCenterY( dragArea, btnClose );
			
			btnMaximum.x = btnClose.x - btnMaximum.width - 2;			
			btnMaximum.y = btnClose.y;
			
			btnPrevSize.x = btnMaximum.x;
			btnPrevSize.y = btnMaximum.y;
			
			btnMinimize.x = btnMaximum.x - btnMinimize.width -2;
			btnMinimize.y = btnClose.y;
			
			btnOption.x = btnMinimize.x - btnOption.width -2;
			btnOption.y = btnClose.y;
			
			btnInfo.x = btnMinimize.x - btnInfo.width - 2;
			btnInfo.y = btnClose.y;
			
			dragArea.y = 0;
			dragArea.x = 0;
			
			btnResize.x = bg.width - btnResize.width - 3;
			btnResize.y = bg.height - btnResize.height - 3;
		}
		private function resize( _width: int, _height: int ): void
		{
			bg.width 	= _width;
			bg.height	= _height;
		}
		override public function set width(value:Number):void
		{
			bg.width = value;
			dragArea.width = value - 2;
		}
		override public function set height(value:Number):void
		{
			bg.height = value;
		}
		private function onDragEventListener( e: MouseEvent ): void
		{
			stage.nativeWindow.startMove();
		}
		private function onResizeListener( e: MouseEvent ): void
		{
			switch( e.type )
			{
				case MouseEvent.MOUSE_DOWN:
					stage.nativeWindow.startResize();
					break;
				case MouseEvent.MOUSE_UP:
					//stage.nativeWindow.st
					break;
			}
		}
		public function setSize( w: int, h: int ): void
		{
			/* bg.width = w;
			bg.height = h;
			
			dragArea.width = w - 2;
			bgShadow.width = dragArea.width;
			
			this.setPosition(); */
		}
		private function onMouseEventHandler( e: MouseEvent ): void
		{
			switch( e.currentTarget ) 
			{
				case btnClose:
					this.dispatchEvent( new WindowEvent( WindowEvent.WIN_CLOSE ) );
					this.stage.nativeWindow.close();
					break;
				case btnMinimize:
					this.stage.nativeWindow.minimize();
					break
				case btnMaximum:
					this.stage.nativeWindow.maximize();
					btnMaximum.visible = false;
					btnPrevSize.visible = true;
					break;
				case btnPrevSize:
					this.stage.nativeWindow.restore();
					btnMaximum.visible = true;
					btnPrevSize.visible = false;
					break;
				case btnOption:
					break;
				case btnInfo:
					break;
			}
		}
		override public function get width():Number
		{
			return bg.width;
		}
		override public function get height():Number
		{
			return bg.height;
		}
	}
}